package cn.kgc.controller;

import cn.kgc.base.dto.AdminUserDTO;
import cn.kgc.base.entity.JwtToken;
import cn.kgc.commons.annotation.IgnoreResultAdviceAnnotation;
import cn.kgc.commons.constant.HttpExceptionCodeEnum;
import cn.kgc.commons.constant.SystemConstant;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.RedisUtil;
import cn.kgc.entity.UmsMember;
import cn.kgc.service.JwtService;
import cn.kgc.service.UmsMemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author 王跃
 */
@RequestMapping("/auth")
@Api
@RestController
public class AuthorityController {

    @Resource
    private JwtService jwtService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UmsMemberService umsMemberService;

    @PostMapping("/getToken")
    @ApiOperation(value = "登录获得令牌",httpMethod = "GET",
            produces = "application/json;charset=utf-8",response = JwtToken.class)
    @IgnoreResultAdviceAnnotation
    @CrossOrigin
    public JwtToken  getToken(@RequestBody AdminUserDTO userDTO) throws Exception{
        String token= jwtService.token(userDTO.getUsername(),
                userDTO.getPassword());
        if(token!=null){
            JwtToken jwtToken=new JwtToken();
            jwtToken.setToken(token);
            return jwtToken;
        }else{
            return null;
        }
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册并获得令牌",httpMethod = "POST",
            produces = "application/json;charset=utf-8",response = JwtToken.class)
    @IgnoreResultAdviceAnnotation
    @CrossOrigin
    public JwtToken  register(@RequestBody AdminUserDTO userDTO) throws Exception{
        //判断账号是否重复
        LambdaQueryWrapper<UmsMember> lambdaQueryWrapper
                            =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMember::getUsername,userDTO.getUsername());
        int count=umsMemberService.count(lambdaQueryWrapper);
        if(count>0){
            throw new HttpException(HttpExceptionCodeEnum.USERNAME_REPEAT.getCode(),
                    HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        String key= SystemConstant.REGISTER_PHONE_CODE +userDTO.getPhone();
        if(redisUtil.hasKey(key)){  //如果有key
            Integer tempCode= (Integer)redisUtil.get(key);
              if(tempCode.toString().equals(userDTO.getCode())){  //如果验证码正确,注册用户
                 String token=  jwtService.register(userDTO.getUsername(),
                         userDTO.getPassword());
                 JwtToken jwtToken=new JwtToken();
                 jwtToken.setToken(token);
                 return jwtToken;
              }
        }
        throw new HttpException(HttpExceptionCodeEnum.PHONE_CODE_ERROR.getCode(),
                HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}